<template>
    <div>
        <div @click="popup.openPopup" class="vue-flow-model">
            <n-icon size="30" color="#18A058">
                <Edit/>
            </n-icon>
            <Handle id="EditColumn-inlet" type="target" :position="Position.Left"
                    :is-valid-connection="props.isValidSourcePos"/>
            <Handle id="EditColumn-export" type="source" :position="Position.Right"
                    :is-valid-connection="props.isValidTargetPos"/>
        </div>
        <span>编辑列</span>
        <flow-popup-window
            :show="popup.show"
            :on-click-outside="popup.clickPopupOutside"
            :x="popup.x"
            :y="popup.y"
        >
            <n-card title="编辑列">
                <h4>删除列</h4>
                <n-select
                    v-model:value="operation.delete_column.column"
                    :options="props.data.data.title"
                    @update:value="popup.reopen"
                    multiple
                    placeholder="字段列"
                    label-field="title"
                    value-field="key"
                />
                <n-radio-group v-model:value="operation.delete_column.delete_way" name="radioGroup">
                    <n-space>
                        <n-radio :value="'selected'">
                            删除选中列
                        </n-radio>
                        <n-radio :value="'outside'">
                            删除选中列之外的其它列
                        </n-radio>
                    </n-space>
                </n-radio-group>
                <n-divider />
                <h4>修改列名称</h4>
                <div class="a" v-for="(index) in operation.rename_column">
                    <n-select
                        v-model:value="index.key"
                        :options="props.data.data.title"
                        @update:value="popup.reopen"
                        placeholder="字段列"
                        label-field="title"
                        value-field="key"
                    />
                    <n-input
                        v-model:value="index.new_name"
                        placeholder="输入新列名称"
                    />
                </div>
                <template #footer>
                    <n-space>
                        <n-button @click="getSuperiorData">获取上级节点数据</n-button>
                        <n-button @click="addRename_column">新增条件</n-button>
                        <n-button @click="implementation">确定</n-button>
                    </n-space>
                </template>
            </n-card>
        </flow-popup-window>
    </div>
</template>

<script setup>

import {Handle, Position} from "@vue-flow/core";
import {Edit} from "@vicons/carbon";
import {useElementsStore} from "@/stores/elements";
import workApi from "@/http/workApi";
import FlowPopupWindow from "@/components/VueFlowModel/vueFlowPopupWindow/FlowPopupWindow.vue";

const props = defineProps(['isValidTargetPos', 'isValidSourcePos', 'id', 'data'])
const elements = useElementsStore()

const operation = reactive({
    delete_column: {
        column:[],
        delete_way:"selected"
    },
    rename_column: [
        {key:null,new_name:""}
    ],
})

function addRename_column() {
    operation.rename_column.push(
        {key:null,new_name:""}
    )
}
function implementation() {
    workApi.post("perform_tasks/edit_column",{
        data:props.data.data,
        operation
    }).then(r=>{
        operation.delete_column.column = []
        props.data.data = r.data
    })
}
// 点击弹窗
const popup = reactive({
    show: false,
    x: null,
    y: null,
    wait: false,
    clickPopupOutside: () => {
        popup.closedPopup()
    },
    openPopup: (e) => {
        popup.x = e.x
        popup.y = e.y
        popup.show = true
    },
    closedPopup: () => {
        if (!popup.wait) {
            console.log(22)
            popup.show = false;
        }
    },
    reopen: () => {
        popup.show = true
    }
})

const getSuperiorData = () => {
    // 获取上级节点数据
    for (let i of elements.elements) {
        if (i['target'] === props.id) {
            for (let j of elements.elements) {
                if (j.id === i['source']) {
                    props.data.data = {...j.data.data}
                    props.data.data.title.map(s => {
                        // 移除可修改
                        delete s.render
                        return s
                    })
                    return
                }
            }
        }
    }
}

</script>

<style scoped lang="less">
.a{
    display: flex;
}
</style>